//
//  ForeignCollectHandle.m
//  GoodBuy
//
//  Created by dlios on 15-5-25.
//  Copyright (c) 2015年 GoodBuy. All rights reserved.
//

#import "ForeignCollectHandle.h"

@implementation ForeignCollectHandle
//+(instancetype)shareForeignHandle
//{
//    static ForeignCollectHandle *foreignCollectHandle = nil;
//    if (foreignCollectHandle == nil) {
//        foreignCollectHandle = [[ForeignCollectHandle alloc] init];
//    }
//    return foreignCollectHandle;
//}

+(ForeignCollectHandle *)shareInstance
{
    static ForeignCollectHandle *dbManager = nil;
    if (dbManager == nil) {
        dbManager = [[ForeignCollectHandle alloc] init];
    }
    return dbManager;
}


//static sqlite3 *db = nil;


- (void)openMc
{
//    if (db != nil) {
//        NSLog(@"数据库已经打开了");
//        return;
//    }
    // 创建本地文件路径
    NSArray * array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSLog(@"array   %@",[array lastObject]);
    NSString *documentPath = [array lastObject];
    NSString *squlistFilePath = [documentPath stringByAppendingPathComponent:@"foreign.db"];
//    NSLog(@"%@",squlistFilePath);
    
    // 创建数据库
    // 将本地文件和数据库对象关联在一起
    BOOL result = sqlite3_open(squlistFilePath.UTF8String, &dbPoint);
    if (result == SQLITE_OK) {
        NSLog( @"数据库打开成功");
        
    }else{
        NSLog(@"打开失败");
    }
   
}


#warning  mark - 创建表
- (void)createMC
{
    //1.创建aql语句
    NSString *createSql = @"create table if not exists foreigns (num integer primary key autoincrement,article_id text, article_pic text, article_mall text, article_date text, article_link text, article_price text, article_title text, article_url text)";//

    //2.执行sql语句
    int result = sqlite3_exec(dbPoint, createSql.UTF8String, NULL, NULL, nil);
    NSLog(@"%d", result);
    if (SQLITE_OK == result) {
        NSLog( @"创建表成功");
    }else{
        NSLog( @"创建表失败");
    }
 
}

// 插入数据
- (void)insertForeign:(ForeignCOntroller *)Foreign
{
    NSString *insertSql = [NSString stringWithFormat:@"insert into foreigns (article_id, article_pic, article_mall, article_date, article_link, article_price, article_title, article_url) values ('%@','%@','%@','%@','%@','%@','%@','%@')",Foreign.article_id, Foreign.article_pic, Foreign.article_mall, Foreign.article_date, Foreign.article_link, Foreign.article_price, [Foreign.article_title stringByReplacingOccurrencesOfString:@"'" withString:@" "], Foreign.article_url];


    //执行
//    NSLog(@"ceshQQQQQ%@", insertSql);
    if (Foreign.article_id.length == 0) {
        
    }else{

//        NSLog(@"%@",Foreign.article_id);
        int result = sqlite3_exec(dbPoint, insertSql.UTF8String, NULL, NULL, nil);
        
        if (result == SQLITE_OK) {
            NSLog(@"插入成功");
        }else{
            NSLog(@"插入失败%d",result);
        }
    }
}

- (NSMutableArray *)selectAllForeign
{
    NSString *selectSql = @"SELECT * FROM foreigns";
    // 创建数据库跟随指针
    sqlite3_stmt *stmt = nil;
    // 查询数据库之前准备工作
    // -1 代表不限制sql语句长度
    // 将数据库对象db、跟随指针stmt、sql语句关联起来进行循环查询工作
    NSInteger result = sqlite3_prepare_v2(dbPoint, selectSql.UTF8String, -1, &stmt, nil);
    NSLog(@"result = %ld", result);
    if (result == SQLITE_OK) {
        NSLog(@"查询准备成功");
        
        NSMutableArray *array = [NSMutableArray array];
        // start循环查询每一条数据
        while (sqlite3_step(stmt) == SQLITE_ROW) {
         
            int number = sqlite3_column_int(stmt, 0);
            const unsigned char *article_id = sqlite3_column_text(stmt, 1);
            const unsigned char *article_pic = sqlite3_column_text(stmt, 2);
            const unsigned char *article_mall = sqlite3_column_text(stmt, 3);
            const unsigned char *article_date = sqlite3_column_text(stmt, 4);
            const unsigned char *article_link = sqlite3_column_text(stmt, 5);
            const unsigned char *article_price  = sqlite3_column_text(stmt, 6);
            const unsigned char *article_title = sqlite3_column_text(stmt, 7);
            const unsigned char *article_url = sqlite3_column_text(stmt, 8);
            ForeignCOntroller *foreign = [[ForeignCOntroller alloc] init];
            foreign.article_id = [NSString stringWithUTF8String:(const char *)article_id];
            foreign.article_pic = [NSString stringWithUTF8String:(const char *)article_pic];
            foreign.article_mall = [NSString stringWithUTF8String:(const char *)article_mall];
            foreign.article_date = [NSString stringWithUTF8String:(const char *)article_date];
            foreign.article_link = [NSString stringWithUTF8String:(const char *)article_link];
            foreign.article_price = [NSString stringWithUTF8String:(const char *)article_price];
            foreign.article_title = [NSString stringWithUTF8String:(const char *)article_title];
            foreign.article_url = [NSString stringWithUTF8String:(const char *)article_url];
            
            [array addObject:foreign];
            
            
        }
        return array;
    }else{
        NSLog(@"查询失败");
    }
    
    return nil;
    
}

- (void)dropTable
{
    NSString *dropAql = @"drop table foreigns";
    BOOL result = sqlite3_exec(dbPoint, dropAql.UTF8String, NULL, NULL, nil);
    if (result == SQLITE_OK) {
        NSLog(@"删除成功");
        
    }else{
        NSLog(@"删除失败");
    }
}
@end
